package visaoInterface;

import dao.DaoTipoVeiculo;
import dao.DaoVeiculo;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.*;

public class RegistroVeiculosDisponiveis extends javax.swing.JDialog {
    
    //definição do modelo da tabela
     DefaultTableModel tmVeiculos = new DefaultTableModel(null, new String[]{"Modelo","Marca","Placa","Cor","Tipo do veiculo"} ) {
        @Override
        public boolean isCellEditable(int row, int col)
        {
            return false;
        } 
    };

    /** Creates new form RegistroVeiculosDisponiveis */
    public RegistroVeiculosDisponiveis(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        DaoTipoVeiculo dtv = new DaoTipoVeiculo();
        Vector<TipoVeiculo> resultado = dtv.buscarTodos();
        for (Iterator<TipoVeiculo> iterator = resultado.iterator(); iterator.hasNext();) {  
         TipoVeiculo tv = iterator.next();  
           
         this.jCBTipoVeiculo.addItem(tv.getTipoVeiculo());
      }
    }
    /**
     * mostra dados da pesquisa na tabela
     * @param veiculo  veiculo com os dados do veiculo.
     * @param tipoveiculo tipo veiculos com os dados do tipo veiculo.
     */
     public void mostraPesquisa(Vector<Veiculo> veiculo, Vector<TipoVeiculo> tipoveiculo){
        String[] linha = new String[] {null,null,null};
        
        //zera o jTable
        while(tmVeiculos.getRowCount()>0){
            tmVeiculos.removeRow(0);
        }   
        
            for (int i=0 ; i<veiculo.size() ; i++) {
            tmVeiculos.addRow(linha);
            //"Modelo","Marca","Placa","Cor","Tipo do veiculo"
            tmVeiculos.setValueAt(veiculo.get(i).getModelo(),i,0);
            tmVeiculos.setValueAt(veiculo.get(i).getMarca(),i,1);
            tmVeiculos.setValueAt(veiculo.get(i).getPlaca(),i,2);
            tmVeiculos.setValueAt(veiculo.get(i).getCor(),i,3);
            tmVeiculos.setValueAt(tipoveiculo.get(i).getTipoVeiculo(),i,4);
            
            } 
        if (tmVeiculos.getRowCount() == 0){
            JOptionPane.showMessageDialog(null, "Registro nao encontrado.");
        }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableVeiculos = new javax.swing.JTable();
        jCBTipoVeiculo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jBPesquisa = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Veículos disponíveis"));

        jTableVeiculos.setModel(tmVeiculos);
        jScrollPane1.setViewportView(jTableVeiculos);

        jCBTipoVeiculo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos veiculos" }));

        jLabel1.setText("Selecione o tipo de veiculo a ser pesquisado:");

        jBPesquisa.setText("Pesquisar");
        jBPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBPesquisaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jCBTipoVeiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 181, Short.MAX_VALUE)
                        .addComponent(jBPesquisa)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jCBTipoVeiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBPesquisa))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jBPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBPesquisaActionPerformed
// TODO add your handling code here:
        DaoVeiculo dv = new DaoVeiculo();
        DaoTipoVeiculo dtv = new DaoTipoVeiculo();
        int cod;
        Vector<Veiculo> resultados = new Vector<Veiculo>();
        Vector<TipoVeiculo> resultados2 = new Vector<TipoVeiculo>();
        if (jCBTipoVeiculo.getSelectedItem().toString().equals("Todos veiculos")){
        resultados = dv.buscarVeiculosDisponiveis();
        }else{
            cod = dv.buscarCodTipoVeiculo(jCBTipoVeiculo.getSelectedItem().toString());
            resultados = dv.buscarVeiculosDeDeterminadoTipo(cod);
        }
        resultados2 = dtv.buscarTipoDeDeterminadoVeiculo(resultados);
        this.mostraPesquisa(resultados, resultados2);   
}//GEN-LAST:event_jBPesquisaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RegistroVeiculosDisponiveis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RegistroVeiculosDisponiveis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RegistroVeiculosDisponiveis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RegistroVeiculosDisponiveis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                RegistroVeiculosDisponiveis dialog = new RegistroVeiculosDisponiveis(new javax.swing.JDialog(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBPesquisa;
    private javax.swing.JComboBox jCBTipoVeiculo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableVeiculos;
    // End of variables declaration//GEN-END:variables
}
